package com.xunchi.haichuan.user.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.netai.safedoc.utility.TransactionWrapper;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xunchi.haichuan.audit.service.LogService;
import com.xunchi.haichuan.audit.service.LogServiceImpl;
import com.xunchi.haichuan.configurator.model.SystemConfigs;
import com.xunchi.haichuan.user.model.User;
import com.xunchi.haichuan.user.service.UserService;
import com.xunchi.haichuan.user.service.UserServiceImpl;
import com.xunchi.haichuan.utility.Digest;
import com.xunchi.haichuan.utility.MiscUtil;

/**
 * 作者：kongxb<br>
 * 模块名称：用户模块<br>
 * 功能描述：登录<br>
 * 维护记录：
 */
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 7044408160054979122L;

	/**
	 * 日志
	 */
	private static Log logger = LogFactory.getLog(LoginAction.class);

	private transient String loginName;
	private transient String loginPassword;
	private transient int type;
	/**
	 * 用户 Service
	 */
	private UserService userService;
	/**
	 * 日志 Service
	 */
	private LogService logService;

	/**
	 * Struts2默认执行方法
	 */
	public String execute() throws Exception {
		return Action.SUCCESS;
	}

	/**
	 * 普通用户登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String logon() throws Exception {
		logger.debug("Begin logon()");
		return doLogon();
	}

	/**
	 * 管理员登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String adminLogon() throws Exception {
		logger.debug("Begin adminLogon()");
		return doLogon();
	}

	private String doLogon() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();

		userService = (UserService) TransactionWrapper.decorate(new UserServiceImpl(), false);
		User user = userService.getUser(loginName, Digest.getSHA1Digest(loginPassword), type);
		if (user == null) {
			this.addFieldError("loginName", this.getText("USER.error.login.notMathch"));
			return Action.INPUT;
		}
		user.setName(MiscUtil.replaceMsgKey(user.getName(), request.getLocale()));
		user.getType(); // 取得用户类型
		user.setPermissions(userService.getPermissions(user.getUsrId())); // 设置用户权限
		User currentUser = new User();
		try {
			BeanUtils.copyProperties(currentUser, user);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		Map session = ActionContext.getContext().getSession();
		session.put("currentUser", currentUser);
		session.put("systemConfigs", new SystemConfigs());
		if (SystemConfigs.isLog) {
			logService = (LogService) TransactionWrapper.decorate(new LogServiceImpl(), true);
			logService.login(user, 0, null, request.getRemoteAddr(), request.getLocale());
		}
		return Action.SUCCESS;
	}

	/**
	 * 普通用户登出
	 * 
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception {
		logger.debug("Begin logout()");
		return doLogout();
	}

	/**
	 * 管理员登出
	 * 
	 * @return
	 * @throws Exception
	 */
	public String adminLogout() throws Exception {
		logger.debug("Begin adminLogout()");
		return doLogout();
	}

	public String doLogout() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession();

		if (SystemConfigs.isLog) {
			User currentUser = (User) session.get("currentUser");
			if (currentUser == null) {
				return Action.SUCCESS;
			}
			logService = (LogService) TransactionWrapper.decorate(new LogServiceImpl(), true);
			logService.logout(currentUser, 0, null, request.getRemoteAddr(), request.getLocale());
		}
		session.clear();
		return Action.SUCCESS;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}

	public void setType(int type) {
		this.type = type;
	}

}
